package cn.wolfcode.ssm.service.impl;

import cn.wolfcode.ssm.domain.Customer;
import cn.wolfcode.ssm.domain.Employee;
import cn.wolfcode.ssm.mapper.CustomerMapper;
import cn.wolfcode.ssm.query.QueryObject;
import cn.wolfcode.ssm.service.ICustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;


    @Override
    public void save(Customer customer) {
        // 拿到当前登陆对象
        Employee employee = (Employee)SecurityUtils.getSubject().getPrincipal();
        // 给录入人(inputUser属性设置值)
        customer.setInputUser(employee);
        // 给出售的人员(seller设置值)
        customer.setSeller(employee);
        // 给录入时间设置值
        customer.setInputTime(new Date());

        customerMapper.insert(customer);
    }

    @Override
    public void delete(Long id) {
        customerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Customer customer) {
        customerMapper.updateByPrimaryKey(customer);
    }

    @Override
    public Customer get(Long id) {
        return customerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Customer> listAll() {
        return customerMapper.selectAll();
    }

    @Override
    public PageInfo<Customer> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"input_time desc"); //对下一句sql进行自动分页
        List<Customer> customers = customerMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Customer>(customers);
    }

    @Override
    // 新增时候验证手机号码是否存在
    public Customer selectCheckTel(String tel) {
        return customerMapper.selectCheckTel(tel);
    }

    @Override
    // 编辑时候验证用户手机号码是否修改
    public String selectTelById(Long id) {
        return customerMapper.selectTelById(id);
    }

    @Override
    // 修改客户状态
    public void updateStatus(Long id, Integer status) {
        customerMapper.updateStatus(id,status);
    }

    @Override
    //  客户移交转移销售人员
    public void updateSeller(Long sellerId, Long customerId) {
        customerMapper.updateSeller(sellerId,customerId);
    }

    @Override
    public List<Customer> selectStatus() {
        return customerMapper.selectStatus();
    }

}
